//
//  SYS_ShipmentVC.m
//  System_App
//
//  Created by 吴健 on 2017/12/5.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_ShipmentVC.h"
#import "SYS_ShipmentModel.h"
#import "SYS_OrderDetailAddressView.h"
#import "SYS_ShipGoodsView.h"

@interface SYS_ShipmentVC ()<UIScrollViewDelegate>
{
    NSString *sOrderId; //订单ID
    NSString *session;
    NSString *pid;
    
    SYS_ShipmentModel *shipmentsModel;
}


/**
 *  主视图scrollview
 */
@property (nonatomic, strong) UIScrollView *mainScrollView;

/**
 *  地址
 */
@property (nonatomic, strong) SYS_OrderDetailAddressView *addressView;

/**
 *  商品
 */
@property (nonatomic, strong) SYS_ShipGoodsView *goodsView;
/**
 *  发货按钮
 */
@property (nonatomic, strong) UIButton *shipButton;
@end

@implementation SYS_ShipmentVC

- (id)initWithOrderId:(NSString *)orderId{
    if (self == [super init]) {
        sOrderId = orderId;
        [self setUI];
        [self initWithData];
        [self getDeliverGoodsRequest];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationBar];
}

#pragma mark -- setter and getter
- (void)setNavigationBar{
    self.title = @"发货";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"SYS_Public_back" highImage:nil title:nil target:self action:@selector(backToPrevious)];
}

- (void)initWithData{
    session = [SYS_LoginModel instance].sessionInfo.session;
    UICKeyChainStore *keychainStore = [UICKeyChainStore keyChainStore];
    pid = [keychainStore stringForKey:@"pid"];
}

- (void)setUI{
    self.view.backgroundColor = DSCOLOR_GRAY_BACKGROUND;
    [[WHC_KeyboardManager share] addMonitorViewController:self];
    //当前布局高度
    float height=0;
    
    _mainScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-64)];
    _mainScrollView.backgroundColor = DSCOLOR_GRAY_BACKGROUND;
    _mainScrollView.delegate=self;
    _mainScrollView.showsHorizontalScrollIndicator=NO;
    _mainScrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_mainScrollView];
    
    //地址信息
    _addressView=[[SYS_OrderDetailAddressView alloc] initWithFrame:CGRectMake(0, height, SCREENWIDTH, 90)];
    _addressView.isShowNote = YES;
    [_mainScrollView addSubview:_addressView];
    height+=90;
    
    _goodsView = [[SYS_ShipGoodsView alloc] init];
    [_mainScrollView addSubview:_goodsView];
    
    _addressView.sd_layout
    .leftSpaceToView(_mainScrollView, 0)
    .rightSpaceToView(_mainScrollView, 0)
    .topSpaceToView(_mainScrollView, 10)
    .autoHeightRatio(0);
    
    _goodsView.sd_layout
    .leftSpaceToView(_mainScrollView, 0)
    .rightSpaceToView(_mainScrollView, 0)
    .topSpaceToView(_addressView, 10)
    .autoHeightRatio(0);
    
    [_mainScrollView setupAutoHeightWithBottomView:_goodsView
                                      bottomMargin:10];
}

//刷新界面
- (void)refreshUI{
    _addressView.backgroundColor = WJCOLOR_WHITE;
    _addressView.lb_name.text    = FORMAT(@"收货人：%@", shipmentsModel.shippingAddressName);
    _addressView.lb_phone.text   = shipmentsModel.shippingAddressContact;
    _addressView.lb_address.text = shipmentsModel.shippingAddress;
    _addressView.note.text    = shipmentsModel.note.length == 0? @"买家备注:": FORMAT(@"买家备注:%@", shipmentsModel.note);
    _addressView.note.height  = [DSUtility heightOfString:_addressView.note.text font:DSFONT_DETAIL width:SCREENWIDTH maxHeight:999];
    
    _goodsView.shipmentsModel = shipmentsModel;
}

#pragma mark -- touch and event
- (void)backToPrevious{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- HttpRequest
- (void)getDeliverGoodsRequest{
    NSDictionary *params = @{@"session": session, @"orderId": sOrderId, @"pid": pid};
    [SYS_HttpUtility getDeliverGoodsRequest:params success:^(id sender) {
        if ([sender[@"flag"] isEqualToString:@"true"]) {
            NSDictionary *dictionary = sender[@"data"];
            shipmentsModel = [SYS_ShipmentModel mj_objectWithKeyValues:dictionary];
            NSMutableArray *mar_uList = [NSMutableArray new];
            for (int i = 0; i < shipmentsModel.uList.count; i++) {
                UList *uListModel = [UList mj_objectWithKeyValues:shipmentsModel.uList[i]];
                NSMutableArray *mar_orderItems  = [NSMutableArray new];
                NSMutableArray *mar_deliveryWay = [NSMutableArray new];
                NSMutableArray *mar_address     = [NSMutableArray new];
                for (int i = 0; i < uListModel.orderItems.count; i++) {
                    OrderItems *order = [OrderItems mj_objectWithKeyValues:uListModel.orderItems[i]];
                    order.isSelect = YES;
                    [mar_orderItems addObject:order];
                }
                for (int j = 0; j < uListModel.deliveryWayList.count; j++) {
                    DeliveryWayList *list = [DeliveryWayList mj_objectWithKeyValues:uListModel.deliveryWayList[j]];
                    [mar_deliveryWay addObject:list];
                }
                for (int k = 0; k < uListModel.addressList.count; k++) {
                    AddressList *address = [AddressList mj_objectWithKeyValues:uListModel.addressList[k]];
                    [mar_address addObject:address];
                }
                uListModel.addressList     = mar_address;
                uListModel.deliveryWayList = mar_deliveryWay;
                uListModel.orderItems      = mar_orderItems;
                [mar_uList addObject:uListModel];
            }
            shipmentsModel.uList = mar_uList;
            [self refreshUI];
        }else{
            [DSUtility mbSay:sender[@"exp"]];
        }
    } failure:^(id sender) {
        RequestFailure;
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
